// 引入公用的自定义函数
// var $ = require('../../../utils/js.js');
var WxParse = require('../../../wxParse/wxParse.js');
var $ = require('../../../utils/js.js');
var img_gy_src = $.img_gy_src;
const common = require('../../../utils/common.js');
const $dphttp = require('../../../utils/api/dianpu.js');

Page({
  // 页面的初始数据
  data: {
    // 图片路径的公用部分
    img_gy_src: img_gy_src,

    // 当前轮播的小标
    lun_bo_index: 0,
    shop_id: 0,
    // 轮播图的数据
    shop_detail: [],
    //商家id
    id: "",
    star: null,
  },
  // 生命周期函数--监听页面加载
  onLoad: function(options) {
    var that = this;
    if (options.scene){
      var scene = decodeURIComponent(options.scene)
      this.setData({
        id: scene,
      })
      this.shop_detail_fun(scene)
    } else {
      this.setData({
        id: options.id,
      })
      this.shop_detail_fun(options.id)
    }
  },
  // 生命周期函数--监听页面显示
  onShow: function() {
    if (this.data.shua_xin) {
      this.shop_detail_fun(this.data.id)
    } else {
      this.setData({
        shua_xin: true,
      })
    }
  },
  // 自定义函数   开始
  //店铺详情函数
  shop_detail_fun(shop_id) {
    var that = this;
    $.ajax({
      url: "/shops/detail",
      method: "GET",
      data: {
        shop_id: shop_id
      },
      success: function(res) {
        var data = res.data.data;
        console.log("店铺信息", data);
        if (data) {
          // 调用富文本函数
          WxParse.wxParse('fu_wen_ben', 'html', data.desc_content, that);
          var score = data.score;
          data.score = score.toFixed(1);
          that.setData({
            shop_detail: data,
          })
          //将分数四舍五入
          // var star = Math.round(score);
          that.setData({
            xing_ji: [score - 0, score - 1, score - 2, score - 3, score - 4]
          })
        } else {
          that.setData({
            shop_detail: null,
          })
        }
      }
    });
  },
  //轮播改变时
  lb_change_fun(e) {
    this.setData({
      lun_bo_index: e.detail.current
    })
  },
  // 点击轮播图时触发
  lb_yu_lan_fun(e) {
    var src = e.currentTarget.dataset.src;
    var lun_bo_data = this.data.shop_detail.shop_images;

    var imgs = [];
    lun_bo_data.forEach((str) => {
      imgs.push(str);
    })
    $.yu_lan(src, imgs);
  },
  //收藏
  shoucang(e) {
    let id = e.currentTarget.dataset.id
    $dphttp.shops_collect(id, this.data.shop_detail.collect_state == 0 ? 1 : 2).then((res) => {
      $.ti_shi({
        title: res.data.msg
      })
      if (res.data.code == 1) {
        this.setData({
          ['shop_detail.collect_state']: this.data.shop_detail.collect_state == 0 ? 1 : 0
        })
      }
    })
  },
  // 打给店主电话
  shop_phone(e) {
    let shop_phone = e.currentTarget.dataset.shop_phone
    common.tell(shop_phone)
  },
  //领取优惠券
  you_hui_quan(e) {
    var that = this;
    var coupon_id = e.currentTarget.dataset.coupon_id;
    //提交
    $.ajax({
      url: "/shops/add_coupon",
      method: "GET",
      data: {
        coupon_id: coupon_id,
      },
      success: function(res) {
        $.ti_shi({
          title: res.data.msg
        })
        if (res.data.code == 1) {
          that.shop_detail_fun(that.data.id)
        }
      }
    });
  },
  // 点击商品打开商品详情
  open_goods(e) {
    // 获取商品的id
    var id = $.data(e, "id");
    // 获取商品类型并判断
    var prom_type = $.data(e, "prom_type");
    // 0默认1限时抢购2砍价3拼团4清仓5预约服务
    switch (prom_type) {
      case 0:
        $.open("/pages/qiang_gou/xq/xq?id=" + id);
        break;
      case 1:
        $.open("/pages/qiang_gou/xq_qiang_gou/xq_qiang_gou?id=" + id);
        break;
      case 2:
        $.open("/pages/qiang_gou/xq_kan_jia/xq_kan_jia?id=" + id);
        break;
      case 3:
        $.open("/pages/qiang_gou/xq_pin_tuan/xq_pin_tuan?id=" + id);
        break;
      case 4:
        $.open("/pages/qiang_gou/xq_qing_cang/xq_qing_cang?id=" + id);
        break;
      case 5:
        $.open("/pages/qiang_gou/xq_yu_yue/xq_yu_yue?id=" + id);
        break;
    }
  },
  // 打开地图
  open_map() {
    $.open_map(this.data.shop_detail.longitude - 0, this.data.shop_detail.latitude - 0, this.data.shop_detail.shop_address);
  },
  //前往聊天
  liao_tian(e) {
    $.deng_lu(function() {
      var receive_id = $.data(e, "receive_id");
      var title = $.data(e, "title");
      $.open("/pages/my/liao_tian_content/liao_tian_content?user_id=" + getApp().globalData.userinfo.id + '&receive_id=' + receive_id + "&title=" + title);
    })
  },
  // 查看小程序码
  look_img() {
    var src = this.data.shop_detail.qr_code_xcx;
    $.yu_lan(src, [src])
  },
  // 点击我要入驻
  ru_zhu() {
    $.deng_lu(function() {
      $.get_user_data((user_data) => {
        // 获取店铺状态
        var has_shop = user_data.has_shop;
        if (has_shop == 1) { //入驻成功
          $.open("/pages/my/shen_he/shen_he?sh=1&id=" + user_data.shopid);
        } else if (has_shop == 2) { //店铺审核中
          $.open("/pages/my/shen_he/shen_he?sh=2");
        } else { //没有店铺
          $.open("/pages/my/get_my_shop/get_my_shop");
        }
      })
    })
  },
  // 自定义函数   结束
  // 用户点击右上角分享
  onShareAppMessage: function() {
    var that = this;
    var path = "/pages/shang_quan/dian_pu_xiang_qing/dian_pu_xiang_qing?id=" + that.data.id;
    return {
      title: that.data.shop_detail.shop_name,
      path: path,
      // imgUrl: '',//图片长宽比5:4
    }
  },
})